// pages/article/article.js
const mixins = require('../../mixins/article')
const app = getApp()
import config from "../../api/config";
const options = {

  /**
   * 页面的初始数据
   */
  data: {
    posts: [],
    page: 1,
    categoryMap: [{
      name: '全部',
      id: 0
    }],
    catid: 0,
    check: 0,
    header: ["资讯", "栏目"],
    tabcurrent: 0,
    articleload: true,
    activeTagId: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getPosts();
    this.getCategoriesList();
  },


  /**
   * 菜单切换
   */

  changeTab: function (e) {
    let index = e.currentTarget.dataset.index;
    let catid = e.currentTarget.id;
    this.setData({
      activeTagId: index,
      articleload: true,
      catid: catid,
      categoryMap: this.data.categoryMap,
      posts: [],
      page: 1,
      isLastPage: false,
    })
    if (index == 0) {
      this.getPosts({
        page: this.data.page
      });
    } else {
      this.getPosts({
        categories: this.data.categoryMap[index].id,
        page: this.data.page
      });
    }

  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      posts: [],
      page: 1,
      isLastPage: false,
    })
    if (this.data.catid == 0) {
      this.getPosts({
        page: this.data.page
      });
    } else {
      this.getPosts({
        categories: this.data.categoryMap[this.data.activeTagId].id,
        page: this.data.page
      });
    }
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    let index = this.data.activeTagId;
    let page = this.data.page;
    this.setData({
      articleload: true
    })
    if (this.data.isLastPage) {
      if (this.data.catid == 0) {
        this.getPosts({
          page: this.data.page
        });
      } else {
        this.getPosts({
          categories: this.data.catid,
          page: this.data.page
        });
      }
    }
  },




  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

}
mixins(options)
Page(options)